package GUI.frame;

import GUI.pojo.UserDO;
import GUI.service.UserService;
import GUI.service.impl.UserServiceImpl;
import GUI.util.DBUtil;
import GUI.util.Utilimg;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MenuPanel extends JPanel implements ActionListener {
    private ImageIcon imageIcon = Utilimg.getIcon("/img/buttonOff.jpg");
    private ImageIcon imageIcon1 = Utilimg.getIcon("/img/buttonSave.jpg");
    private ImageIcon imageIcon2 = Utilimg.getIcon("/img/buttonGet.jpg");
    private ImageIcon imageIcon3 = Utilimg.getIcon("/img/buttonFind.jpg");
    private ImageIcon imageIcon4 = Utilimg.getIcon("/img/buttonModify.jpg");
    private ImageIcon imageIcon5 = Utilimg.getIcon("/img/pane.jpg");
    private JButton loginjButton;
    private JButton loginjButton1;
    private JButton loginjButton2;
    private JButton loginjButton3;
    private JButton loginjButton4;
    private JButton loginjButton5;
    private Text2 frame;
    private UserService userService=new UserServiceImpl();
    public MenuPanel(Text2 frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        //存款
        loginjButton1 = new JButton(imageIcon1);
        loginjButton1.setBounds(0, 150, 100, 50);
        this.add(loginjButton1);
        loginjButton1.addActionListener(this);
        //取款
        loginjButton2 = new JButton(imageIcon2);
        loginjButton2.setBounds(0, 250, 100, 50);
        this.add(loginjButton2);
        loginjButton2.addActionListener(this);
        //查款
        loginjButton3 = new JButton(imageIcon3);
        loginjButton3.setBounds(0, 350, 100, 50);
        this.add(loginjButton3);
        loginjButton3.addActionListener(this);
        //改密
        loginjButton4 = new JButton(imageIcon4);
        loginjButton4.setBounds(690, 150, 100, 50);
        this.add(loginjButton4);
        loginjButton4.addActionListener(this);
        //转账
        loginjButton5 = new JButton(imageIcon5);
        loginjButton5.setBounds(690, 250, 100, 50);
        this.add(loginjButton5);
        loginjButton5.addActionListener(this);
        //退出
        loginjButton = new JButton(imageIcon);
        loginjButton.setBounds(690, 350, 100, 50);
        this.add(loginjButton);
        loginjButton.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == loginjButton) {
            DBUtil db = new DBUtil();
            db.getById(frame.getLoginUser().getId());
            frame.switchPanel("login");
        }else if(e.getSource()==loginjButton1){
            frame.switchPanel("cun");
        }else if(e.getSource()==loginjButton2){
            frame.switchPanel("qu");
        }else if(e.getSource()==loginjButton3){
//            DBUtil db=new DBUtil();
//            UserDO user=db.getById(frame.getLoginUser().getId());
//            frame.getChaPanel().getjTextField().setText(user.getBalance()+"");

            double balance =userService.searchMoney(frame.getLoginUser().getId());
            frame.getChaPanel().getjTextField().setText(balance+"");
            frame.switchPanel("cha");
        }else if(e.getSource()==loginjButton4){
            frame.switchPanel("gai");
        }else if(e.getSource()==loginjButton5){
            frame.switchPanel("zhuan");
        }

    }
}
